package com.mcxx.modules.minimumliving.service.impl;

import cn.hutool.core.util.IdUtil;
import com.mcxx.common.exception.BizException;
import com.mcxx.modules.minimumliving.service.SpecialSignManagementService;
import com.mcxx.modules.base.utils.QueryParamUtil;
import com.mcxx.modules.miniSalvation.special.dto.SpecialSignDTO;
import com.mcxx.modules.miniSalvation.special.entity.MemberSpecialSignEntity;
import com.mcxx.modules.miniSalvation.special.entity.SpecialSignEntity;
import com.mcxx.modules.miniSalvation.special.param.SpecialSignParam;
import com.mcxx.modules.miniSalvation.special.service.read.MemberSpecialSignReadService;
import com.mcxx.modules.miniSalvation.special.service.read.SpecialSignReadService;
import com.mcxx.modules.miniSalvation.special.service.write.MemberSpecialSignWriteService;
import com.mcxx.modules.miniSalvation.special.service.write.SpecialSignWriteService;
import com.mcxx.util.Constant;
import com.yinhai.ta404.core.restservice.requestbean.PageParam;
import com.yinhai.ta404.core.restservice.resultbean.Page;
import com.yinhai.ta404.core.security.vo.UserAccountVo;
import com.yinhai.ta404.core.service.BaseService;
import com.yinhai.ta404.core.transaction.annotation.TaTransactional;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;



@Slf4j
@Service("specialSignManagementService")
@TaTransactional
public class SpecialSignManagementServiceImpl extends BaseService implements SpecialSignManagementService {
    @Autowired
    private SpecialSignReadService specialSignReadService;

    @Autowired
    private SpecialSignWriteService specialSignWriteService;

    @Autowired
    private MemberSpecialSignWriteService memberSpecialSignWriteService;

    @Autowired
    private MemberSpecialSignReadService memberSpecialSignReadService;



    @Override
    public Page querySpecialList(SpecialSignParam param, UserAccountVo userAccountVo, PageParam pageParam) {
        QueryParamUtil.setBaseParamValue(param,userAccountVo);
        return specialSignReadService.querySpecialList(param, pageParam);
    }


    @Override
    public int addSpecialSign(SpecialSignDTO dto, UserAccountVo userAccountVo) {
        int i = specialSignReadService.checkName(dto.getName());
        SpecialSignEntity specialSignEntity = new SpecialSignEntity();
        BeanUtils.copyProperties(dto,specialSignEntity);
        Integer specialValue = 0;
            if(i <= 0){

                Integer max = specialSignReadService.queryMaxSpecialValue();

                if(max <= 0){
                    specialValue += 10;
                }else{
                    specialValue = max + 10;
                }
                specialSignEntity.setStatus(Constant.OrganizationState.DISABLED);
                specialSignEntity.setCreateTime(new Date());
                specialSignEntity.setSpecialValue(specialValue);
                specialSignEntity.setCreateUser(userAccountVo.getUserId());
                specialSignEntity.setId(IdUtil.simpleUUID());
                int rst = specialSignWriteService.insertIgnoreNull(specialSignEntity);
                if(rst <= 0){
                    log.info("name = {},说明保存特殊标识信息失败", dto.getName());
                    throw new BizException("-1", "保存特殊标识信息失败");
                }
            }else{
                log.info("{}-该名称特殊标识已存在", dto.getName());
                throw new BizException("-1", "该特殊标识名称已存在");
            }


        return 1;
    }


    @Override
    public int updateSpecialSignById(SpecialSignDTO dto, UserAccountVo userAccountVo) {
        SpecialSignEntity specialSignEntity = new SpecialSignEntity();
        BeanUtils.copyProperties(dto,specialSignEntity);
        specialSignEntity.setUpdateTime(new Date());
        specialSignEntity.setUpdateUser(userAccountVo.getUserId());
        int rst = specialSignWriteService.updateIgnoreNull(specialSignEntity);
        if(rst <= 0){
            log.info("name = {},说明保存特殊标识信息失败", dto.getName());
            throw new BizException("-1", "修改特殊标识信息失败");
        }
        return rst;
    }


    @Override
    public void addMemberSpecialSign(SpecialSignDTO dto, UserAccountVo userAccountVo) {

        List<MemberSpecialSignEntity> memberSpecialSignEntities = memberSpecialSignReadService.getMemberSpecialSign(dto.getIdcard());
        if(!CollectionUtils.isEmpty(memberSpecialSignEntities)){
            throw new BizException("-1","该对象已登记过特殊标识");
        }

        MemberSpecialSignEntity memberSpecialSignEntity = new MemberSpecialSignEntity();
        String batch = IdUtil.simpleUUID();
        String[] special = dto.getSpecialValues().split(",");
        for(String value : special){
            memberSpecialSignEntity.setId(IdUtil.simpleUUID());
            memberSpecialSignEntity.setMemberId(dto.getMemberId());
            memberSpecialSignEntity.setSpecialValue(value);
            memberSpecialSignEntity.setIdcard(dto.getIdcard());
            memberSpecialSignEntity.setBatch(batch);
            memberSpecialSignEntity.setStatus(Constant.SpecialSignType.REGISTER);
            memberSpecialSignEntity.setCreateTime(new Date());
            memberSpecialSignEntity.setCreateUser(userAccountVo.getUserId());
            int rst = memberSpecialSignWriteService.insertIgnoreNull(memberSpecialSignEntity);
            if(rst <= 0){
                log.info("name = {},说明保存特殊标识信息失败", dto.getName());
                throw new BizException("-1", "成员特殊标识信息失败");
            }
        }
    }

    @Override
    public Page getAuditSpecialSignList(SpecialSignParam param, UserAccountVo userAccountVo, PageParam pageParam) {
        QueryParamUtil.setBaseParamValue(param,userAccountVo);
        return memberSpecialSignReadService.getAuditSpecialSignList(param, pageParam);
    }

    @Override
    public void updateMemberSpecialSign(SpecialSignDTO dto, UserAccountVo userAccountVo) {

        List<MemberSpecialSignEntity> memberSpecialSignEntities = memberSpecialSignReadService.getMemberSpecialSignEdit(dto.getIdcard(),"'00','02'");
        if(CollectionUtils.isEmpty(memberSpecialSignEntities)){
            throw new BizException("-1","该对象已提交审核");
        }

        memberSpecialSignWriteService.deleteByIdcard(dto.getIdcard(),dto.getMemberStatus());


        MemberSpecialSignEntity memberSpecialSignEntity = new MemberSpecialSignEntity();
        String batch = IdUtil.simpleUUID();
        String[] special = dto.getSpecialValues().split(",");
        for(String value : special){
            memberSpecialSignEntity.setId(IdUtil.simpleUUID());
            memberSpecialSignEntity.setMemberId(dto.getMemberId());
            memberSpecialSignEntity.setSpecialValue(value);
            memberSpecialSignEntity.setIdcard(dto.getIdcard());
            memberSpecialSignEntity.setBatch(batch);
            memberSpecialSignEntity.setStatus(dto.getMemberStatus());
            memberSpecialSignEntity.setCreateTime(new Date());
            memberSpecialSignEntity.setCreateUser(userAccountVo.getUserId());
            memberSpecialSignEntity.setUpdateTime(new Date());
            memberSpecialSignEntity.setUpdateUser(userAccountVo.getUserId());
            int rst = memberSpecialSignWriteService.insertIgnoreNull(memberSpecialSignEntity);
            if(rst <= 0){
                log.info("name = {},说明保存特殊标识信息失败", dto.getName());
                throw new BizException("-1", "成员特殊标识信息失败");
            }
        }
    }


    @Override
    public int deleteSpecialSignByIdcard(String idcard, String memberStatus, UserAccountVo userAccountVo) {
        MemberSpecialSignEntity memberSpecialSignEntity = new MemberSpecialSignEntity();
        memberSpecialSignEntity.setIdcard(idcard);
        memberSpecialSignEntity.setStatus(memberStatus);
        memberSpecialSignEntity.setUpdateTime(new Date());
        memberSpecialSignEntity.setUpdateUser(userAccountVo.getUserId());

        int rst = memberSpecialSignWriteService.updateMemberStatus(memberSpecialSignEntity,Constant.SpecialSignType.INVALID);
        return rst;
    }

    @Override
    public int auditSpecialSignByIdcard(String idcard, String memberStatus, UserAccountVo userAccountVo) {
        MemberSpecialSignEntity memberSpecialSignEntity = new MemberSpecialSignEntity();
        memberSpecialSignEntity.setIdcard(idcard);
        memberSpecialSignEntity.setStatus(memberStatus);
        memberSpecialSignEntity.setUpdateTime(new Date());
        memberSpecialSignEntity.setUpdateUser(userAccountVo.getUserId());

        int rst = memberSpecialSignWriteService.updateMemberStatus(memberSpecialSignEntity,Constant.SpecialSignType.AUDIT);
        return rst;
    }

    @Override
    public Page getConfirmSpecialSignList(SpecialSignParam param, UserAccountVo userAccountVo, PageParam pageParam) {
        QueryParamUtil.setBaseParamValue(param,userAccountVo);
        return memberSpecialSignReadService.getConfirmSpecialSignList(param, pageParam);
    }

    @Override
    public void confirmMemberSpecialSign(SpecialSignDTO dto, UserAccountVo userAccountVo) {

        List<MemberSpecialSignEntity> memberSpecialSignEntities = memberSpecialSignReadService.getMemberSpecialSignEdit(dto.getIdcard(),"'01'");
        if(CollectionUtils.isEmpty(memberSpecialSignEntities)){
            throw new BizException("-1","该对象已完成审核");
        }


        if(!dto.getOldSpecialValues().equals(dto.getSpecialValues())){

            memberSpecialSignWriteService.deleteByIdcard(dto.getIdcard(),dto.getMemberStatus());


            MemberSpecialSignEntity memberSpecialSignEntity = new MemberSpecialSignEntity();
            String batch = IdUtil.simpleUUID();
            String[] special = dto.getSpecialValues().split(",");
            for(String value : special){
                memberSpecialSignEntity.setId(IdUtil.simpleUUID());
                memberSpecialSignEntity.setMemberId(dto.getMemberId());
                memberSpecialSignEntity.setSpecialValue(value);
                memberSpecialSignEntity.setIdcard(dto.getIdcard());
                memberSpecialSignEntity.setStatus(dto.getMemberStatus());
                memberSpecialSignEntity.setCreateTime(new Date());
                memberSpecialSignEntity.setBatch(batch);
                memberSpecialSignEntity.setCreateUser(userAccountVo.getUserId());
                memberSpecialSignEntity.setUpdateTime(new Date());
                memberSpecialSignEntity.setUpdateUser(userAccountVo.getUserId());
                int rst = memberSpecialSignWriteService.insertIgnoreNull(memberSpecialSignEntity);
                if(rst <= 0){
                    log.info("name = {},说明保存特殊标识信息失败", dto.getName());
                    throw new BizException("-1", "成员特殊标识信息失败");
                }
            }

        }
        MemberSpecialSignEntity memberSpecialSignEntitys = new MemberSpecialSignEntity();
        memberSpecialSignEntitys.setIdcard(dto.getIdcard());
        memberSpecialSignEntitys.setStatus(dto.getMemberStatus());
        memberSpecialSignEntitys.setCheckTime(new Date());
        memberSpecialSignEntitys.setCheckUser(userAccountVo.getUserId());
        memberSpecialSignEntitys.setExplain(dto.getExplain());
        if("1".equals(dto.getCheckOption())){
            List<MemberSpecialSignEntity> memberSpecialSignEntitieList = memberSpecialSignReadService.getMemberSpecialSignEdit(dto.getIdcard(),"'10'");
            if(!CollectionUtils.isEmpty(memberSpecialSignEntitieList)){
                MemberSpecialSignEntity memberSpecialSignEntity = new MemberSpecialSignEntity();
                memberSpecialSignEntity.setIdcard(memberSpecialSignEntitieList.get(0).getIdcard());
                memberSpecialSignEntity.setStatus(memberSpecialSignEntitieList.get(0).getStatus());
                memberSpecialSignEntity.setUpdateTime(new Date());
                memberSpecialSignEntity.setUpdateUser(userAccountVo.getUserId());

                memberSpecialSignWriteService.updateMemberStatus(memberSpecialSignEntity,Constant.SpecialSignType.INVALID);
            }
            memberSpecialSignWriteService.updateMemberStatus(memberSpecialSignEntitys,Constant.SpecialSignType.CHECKED);
        }else{
            memberSpecialSignWriteService.updateMemberStatus(memberSpecialSignEntitys,Constant.SpecialSignType.REJECT);
        }

    }

    @Override
    public void changeMemberSpecialSign(SpecialSignDTO dto, UserAccountVo userAccountVo) {

        List<MemberSpecialSignEntity> memberSpecialSignEntities = memberSpecialSignReadService.getMemberSpecialSignEdit(dto.getIdcard(),"'00','02','01'");
        if(!CollectionUtils.isEmpty(memberSpecialSignEntities)){
            throw new BizException("-1","该对象已提交变更");
        }

        MemberSpecialSignEntity memberSpecialSignEntity = new MemberSpecialSignEntity();
        String batch = IdUtil.simpleUUID();
        String[] special = dto.getSpecialValues().split(",");
        for(String value : special){
            memberSpecialSignEntity.setId(IdUtil.simpleUUID());
            memberSpecialSignEntity.setMemberId(dto.getMemberId());
            memberSpecialSignEntity.setSpecialValue(value);
            memberSpecialSignEntity.setIdcard(dto.getIdcard());
            memberSpecialSignEntity.setBatch(batch);
            memberSpecialSignEntity.setStatus(Constant.SpecialSignType.REGISTER);
            memberSpecialSignEntity.setCreateTime(new Date());
            memberSpecialSignEntity.setCreateUser(userAccountVo.getUserId());
            memberSpecialSignEntity.setUpdateTime(new Date());
            memberSpecialSignEntity.setUpdateUser(userAccountVo.getUserId());
            int rst = memberSpecialSignWriteService.insertIgnoreNull(memberSpecialSignEntity);
            if(rst <= 0){
                log.info("name = {},说明保存特殊标识信息失败", dto.getName());
                throw new BizException("-1", "保存成员特殊标识信息失败");
            }
        }
    }
}
